/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package filecopier.console.copy;

import java.io.File;
import java.io.IOException;
import java.util.List;
import filecopier.ActionCommand;
import filecopier.ActionCancelException;
import filecopier.Singleton;
import filecopier.copy.AbstractCopyWorker;
import filecopier.script.Script;
import filecopier.script.RestoreScript;
import org.apache.log4j.Logger;
import custom.util.FileCopyHelper;
import custom.util.FileCopyCancelException;
import custom.xml.DOMHelperException;

/**
 *
 * @author david
 */
public class CopyConsoleWorker extends AbstractCopyWorker implements ActionCommand {

    private Logger logger = null;

    public CopyConsoleWorker(final Script fromScript, final Script toScript) {
        super(fromScript, toScript);
        logger = Singleton.getInstance().getFileConsoleLogger();
    }

    public void doAction() throws ActionCancelException {
        copyFiles();
    }

    public void copyFiles() throws ActionCancelException {
        if (fromScript != null && toScript != null) {
            Script restoreScript = null;
            File restoreScriptFile = null;

            if (Singleton.getInstance().getOptions().buildRestoreScript()) {
                restoreScriptFile = buildDefaultRestoreScriptFile(fromScript, toScript);
                try {
                    restoreScript = new RestoreScript();
                } catch (DOMHelperException e) {
                    // Log the exception, notify user that a restore script will not be built, and set restoreScript to null.
                    logger.error("The restore script cannot be built", e);
                    restoreScript = null;
                }

            }
            FileCopyHelper fileCopyHelper = new FileCopyConsoleHelper(restoreScript);
            try {
                List<File> fromFiles = getFiles(fromScript);
                List<File> toFiles = getFiles(toScript);

                int fileCt = countFiles(fromFiles, toFiles, Singleton.getInstance().getOptions());

                int copyCt = getFileCopy(fileCopyHelper).copy(fromFiles, toFiles);

                if (restoreScript != null) {
                    if (fileCopyHelper.getRestoreCount() > 0) {
                        try {
                            restoreScript.saveToDisk(restoreScriptFile);
                            logger.info("Files that were renamed can be restored by running the restore script \"" +
                                    restoreScript.getScriptName() + "\".");
                        } catch (DOMHelperException e) {
                            logger.error("The restore script + \"" + restoreScript.getScriptName() +
                                    "\" could not be saved", e);
                        } catch (IOException e) {
                            logger.error("The restore script + \"" + restoreScript.getScriptName() +
                                    "\" could not be saved", e);
                        }
                    } else {
                        // I may not have to call delete because the file may have not been created.
                        restoreScriptFile = restoreScript.getScriptFile();
                        if (restoreScriptFile != null) {
                            restoreScriptFile.delete();
                        }
                    }
                }
                if (copyCt == fileCt) {
                    if (fromScript.isTemp() && fromScript.getScriptFile().delete()) {
                        logger.info("Temporary copy script file \"" + fromScript.getScriptName() + "\" was deleted.");
                    }
                    if (toScript.isTemp() && toScript.getScriptFile().delete()) {
                        logger.info("Temporary copy script file \"" + toScript.getScriptName() + "\" was deleted.");
                    }
                }
            } catch (FileCopyCancelException e) {
                throw new ActionCancelException(e);
            }
        }
    }
}
